/*
 * The irq_handler.c file implements IRQ management functions.
 */

#include <types.h>
#include <interrupts.h>
#include <i386_arch.h>  
#include <i386_pmode.h>  
#include <pic.h>
#include <klib.h>  
#include <panic.h> 

// IRQ Table
dword_t irq_table[16];

void initialize_IRQ(void)
  {
    int i;

    // mask all interrupts
    irq_disable_all();

    for (i=0;i<16;i++)
      irq_table[i]=(dword_t)0;
  }

// Handle IRQ events
void handle_IRQ(uregs_t *uregs)
  {
    int irq_num;
    // declare a function pointer to handle IRQ ISR
    void (*isr)(void);

    irq_num=uregs->interrupt_number-IRQ00_VECTOR;
    isr=(void (*)(void))(irq_table[irq_num]);

    /*
    if (spurious_IRQ(irq_num))
      {
	printf("Spurious IRQ: %d\n",irq_num);
	panic("Halting");
      }
    */

    /*
    if (irq_num!=0)
      {
	printf("I got IRQ=0x%x (%d) for which ISR address is 0x%x\n",irq_num,irq_num,(dword_t)isr);
	context_dump(uregs);
	printf("Halting...");
	halt();
      }
    */

    if (!spurious_IRQ(irq_num))
      isr();
  }


void assign_IRQ(int irq_num,dword_t isr)
  {
    irq_table[irq_num]=isr;
    irq_enable(irq_num);
  }


void unassign_IRQ(int irq_num)
  {
    irq_disable(irq_num);
  }


/*
void assign_IRQ(int irq_num,dword_t isr)
  {
    IDT_Entry_T desc;
    int vec_num;

    vec_num=irq_num+IRQ00_VECTOR;
    
    setup_IDT_Entry(&desc,IDT_TYPE_TRAP,GDT_DESC_CODE,(dword_t)isr);
    place_IDT_Entry(vec_num,&desc);

    irq_enable(irq_num);
  }


void unassign_IRQ(int irq_num)
  {
    irq_disable(irq_num);
  }
*/
